﻿var dictionaryListTableVue = new Vue({
    el: "#dictionaryListTable",
    data: {
        hasNewDictionary: false,      //判断是否正在新建数据字典
        hasEditDictionary: false,     //判断是否正在编辑数据字典
        dictionaryList:[]
    },
    methods: {
        AddDictionary: function () {
            AddDictionaryDetail(this);
        },
        SaveDictionary: function (currentDictionary) {
            SaveDictionaryDetail(this, currentDictionary);
        },
        CancelSaveDictionary: function (currentDictionary) {
            CancelSaveDictionaryDetail(this, currentDictionary);
        },
        EditDictionary: function (currentDictionary) {
            EditDictionaryDetail(this, currentDictionary);
        },
        UpdateDictionary: function (currentDictionary) {
            UpdateDictionaryDetail(this, currentDictionary);
        },
        CancelUpdateDictionary: function (currentDictionary) {
            CancelUpdateDictionaryDetail(this, currentDictionary);
        },
        DeleteDictionary: function (currentDictionary) {
            DeleteDictionaryDetail(this, currentDictionary);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        ListDictionary(this);
    }
});

/**
 * 查询所有数据字典
 */
function ListDictionary(vue) {
    ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/System/ListSysDictionary?v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.dictionaryList = res.data;

                //禁用所有Input和Select标签
                vue.$nextTick(function () {
                    $("input").prop("disabled", true);
                });
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            HideAzLoading();
        }
    })
}

/**
 * 创建空数据字典
 */
function CreateDictionary() {
    var dictionary = {
        ID: 0,
        Name: "",
        Description: "",
        DKey: "",
        DValue: ""
    };
    return dictionary;
}

/**
 * 增加数据字典
 */
function AddDictionaryDetail(vue) {
    if (vue.hasEditDictionary || vue.hasNewDictionary) {
        toastr.error("请保存正在编辑的数据字典后再新增Please save the data dictionary being edited before adding。");
    } else {
        vue.dictionaryList.push(CreateDictionary());
        vue.hasNewDictionary = true;
    }
}

/**
 * 保存新增数据字典
 */
function SaveDictionaryDetail(vue, dictionary) {
    if (CheckDictionary(dictionary)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/System/SaveSysDictionary",
            data: { dictionary: dictionary },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存数据字典成功Data dictionary saved successfully。");
                    vue.hasNewDictionary = false;
                    ListDictionary(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消新增数据字典
 */
function CancelSaveDictionaryDetail(vue, dictionary) {
    ListDictionary(vue);
    vue.hasNewDictionary = false;
}


/**
 * 编辑数据字典
 */
function EditDictionaryDetail(vue, dictionary) {
    if (vue.hasNewDictionary || vue.hasEditDictionary) {
        toastr.error("请保存正在编辑的数据字典后再新增Please save the data dictionary being edited before adding。");
    } else {
        ShowEditDictionaryButton(vue, dictionary.ID);
    }

}

/**
 * 更新编辑中数据字典
 */
function UpdateDictionaryDetail(vue, dictionary) {
    if (CheckDictionary(dictionary)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/System/UpdateSysDictionary",
            data: { dictionary: dictionary },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新数据字典信息成功Successfully updated data dictionary information。");
                    HideEditDictionaryButton(vue, dictionary.ID);
                    ListDictionary(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消编辑中数据字典
 */
function CancelUpdateDictionaryDetail(vue, dictionary) {
    HideEditDictionaryButton(vue, dictionary.ID);
}

/**
 * 删除编辑中数据字典
 */
function DeleteDictionaryDetail(vue, dictionary) {
    var message = "确认删除数据字典Confirm deletion of data dictionary[" + dictionary.Name + "]?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/System/DeleteSysDictionary",
            data: { DictionaryID: dictionary.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除数据字典信息成功Data dictionary information deleted successfully。");
                    HideEditDictionaryButton(vue, dictionary.ID);
                    ListDictionary(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 显示编辑数据字典按钮
 */
function ShowEditDictionaryButton(vue, dictionaryID) {
    $("#inputArea" + dictionaryID).find("input").prop("disabled", false);
    $("#btnEdit" + dictionaryID).hide();
    $("#btnUpdate" + dictionaryID).show();
    $("#btnCancelUpdate" + dictionaryID).show();
    $("#btnDelete" + dictionaryID).show();
    vue.hasEditDictionary = true;
}

/**
 * 隐藏编辑数据字典按钮
 */
function HideEditDictionaryButton(vue, dictionaryID) {
    $("#inputArea" + dictionaryID).find("input").prop("disabled", true);
    $("#btnEdit" + dictionaryID).show();
    $("#btnUpdate" + dictionaryID).hide();
    $("#btnCancelUpdate" + dictionaryID).hide();
    $("#btnDelete" + dictionaryID).hide();
    vue.hasEditDictionary = false;
}

/**
 * 检查数据字典内容正确性
 */
function CheckDictionary(dictionary) {
    if (!CheckDictionaryName(dictionary))
        return false;
    return true;
}

/**
 * 检查数据字典名字
 */
function CheckDictionaryName(dictionary) {
    $("#inputName" + dictionary.ID).addClass("has-error");
    if (dictionary.Name != null && dictionary.Name.length > 0) {
        $("#inputName" + dictionary.ID).removeClass("has-error");
        return true;
    }
    return false;
}
